// import axios from 'axios'
import _time from '../utils/_time';
import _md5 from '../utils/_md5';
import _base64 from '../utils/_base64';
import _config from "./_config";
import _layer from "./_layer";
import _object from "../utils/_object";

//明钥
const __KEY = "COMMON@KEY.API";
//接口域名
const __url = _config.domain;
//接口超时时间
const __timeout = 180 * 1000;

//type
let __method = {
  _get: 'GET',
  _post: 'POST',
  _put: 'PUT',
  _del: 'DELETE'
};

//content-type
let __content_type = {
  _primary: 'application/x-www-form-urlencoded',
  _file: 'multipart/form-data', //false
  _json: 'application/json;charset=utf-8',
  _xml: 'text/xml'
};

//data-type
let __data_type = {
  _json: 'json',
  _xml: 'XML'
};

//JSON序列化并加密
let __encode = function (params) {
  params = JSON.stringify(params);
  return _base64._encode(params);
};

//解密并JSON反序列化
let __decode = function (response) {
  response = _base64._decode(response);
  return JSON.parse(response);
};

//ajax私有函数
let __ajax = function (url, method, params, data_type, timeout, request_before, response_after) {

  if (typeof request_before == 'function')
  params = request_before(params);

  let data = {};
  if (method === __method._get) {
    data.t = params;
  } else {
    data = params;
  }

  let header = {};
  header['Content-Type'] = __content_type._json;

  return new Promise((reslove, reject) => {
    tt.request({
      url: __url + '/media' + url,
      data: data,
      header: header,
      timeout: timeout,
      method: method,
      dataType: data_type,
      success: function (res) {

        if (res.statusCode == 200 && res.hasOwnProperty('data')) {
          //解析data
          let response = {};
          if (typeof response_after == 'function') {
            response = response_after(res.data);
          } else {
            response = res.data;
          }

          //TODO
          console.log(response);
          //服务器正常响应
          reslove(response);
        } else {
          //http异常 - TODO
          _layer.message('网络异常');
        }
      },
      fail: function (err) {
        console.log(err);
        //提示框 - 网络异常 TODO
        _layer.message('网络异常');
      },
      complete: function (res) {

      }
    });
  });
};



//定义类
let _request = {

  //成功标记
  success: 0,
  //失败标记
  fail: -1,

  //初始化参数头
  _params: function (data = null) {
    let time = _time._current(); //当前时间戳
    let sign = _md5._hex_md5(time + __KEY); //md5加密工具

    let params = {};
    params.time = time;
    params.sign = sign;
    params.data = _object._length(data) > 0 ? data : {};
    params.mini_id = 1;
    return params;
  },

  // get请求
  _get: function (url, data = null) {
    //参数
    const params = this._params(data);
    return __ajax(url, __method._get, params, __data_type._json, __timeout, __encode, __decode);
  },

  //post请求
  _post: function (url, data = null) {
    //参数
    const params = this._params(data);
    return __ajax(url, __method._post, params, __data_type._json, __timeout, __encode, __decode);
  },
 

};

//导出
export default _request;